//路由鉴权
import router from '@/router'
//引入进度条插件
import nprogress from "nprogress"
//引入进度条的样式
import "nprogress/nprogress.css"
//删除加载进度条傍边的小球
nprogress.configure({ showSpinner: false })
//引入仓库
import pinia from "./stores"
import { store } from "@/stores/modules/user/user.ts"
let userStore = store(pinia)
//全局前置首位
router.beforeEach(async (to: any, from: any, next: any) => {
    //浏览器任务栏的名字
    document.title = to.meta.title
    //进度条开始
    nprogress.start()
    let token = userStore.token
    if (token) {
        //登录成功后不能再访问login，指向首页
        if (to.path == '/login' || to.path == "/login/") {

            next({ path: '/' })

        } else {
            if (userStore.adminName) {
                next()
            } else {
                try {
                    await userStore.userInfo()
                    next()
                } catch (error) {
                    next({ path: '/login', query: {redirect: to.path} })
                }
            }
        }
    } else {
        //没有token只能访问登录页，记录你想去的页面
        if (to.path == '/login') {
            next();
        } else {
            next({ path: '/login', query: { redirect: to.path } })
        }
    }

})
//全局后置首位
router.afterEach(() => {
    nprogress.done()
})